package com.beikai.demo01.io;

import java.io.*;

/**
 * Created with IntelliJ IDEA.
 *
 * @author : hanbeikai
 * Date: 2021/5/5 2:29 下午
 * Description: 测试io
 */
public class IoDemo {
    public static void main(String[] args) throws IOException {
        File file = new File("/Users/hanbeikai/Desktop/test.txt");

        File data = new File("/Users/hanbeikai/Desktop/test.txt");
        File a = new File("/Users/hanbeikai/Desktop/test-a.txt");
        File b = new File("/Users/hanbeikai/Desktop/test-b.txt");

        StringBuilder sb = new StringBuilder();

        long start = System.currentTimeMillis();
        copy(data, a);
        long end = System.currentTimeMillis();

        long start2 = System.currentTimeMillis();
        bufferedCopy(data, b);
        long end2 = System.currentTimeMillis();

        System.out.println("普通字节流耗时：" + (end - start) + " ms");
        System.out.println("缓冲字节流耗时：" + (end2 - start2) + " ms");
    }

    // 普通字节流
    public static void copy(File in, File out) throws IOException {
        // 封装数据源
        InputStream is = new FileInputStream(in);
        // 封装目的地
        OutputStream os = new FileOutputStream(out);
        byte[] tempByte = new byte[1024];
        int by = 0;
        while ((by = is.read(tempByte)) != -1) {
            os.write(tempByte,0,by);
        }
        is.close();
        os.close();
    }

    // 缓冲字节流
    public static void bufferedCopy(File in, File out) throws IOException {
        // 封装数据源
        BufferedInputStream bi = new BufferedInputStream(new FileInputStream(in));
        // 封装目的地
        BufferedOutputStream bo = new BufferedOutputStream(new FileOutputStream(out));

        FileWriter fileWriter = new FileWriter(out);


        byte[] tempByte = new byte[1024];
        char[] tempChar = new char[1024];
        int by = 0;
        while ((by = bi.read(tempByte)) != -1) {
            bo.write(tempByte,0,by);
            fileWriter.write(tempChar,0,by);
            fileWriter.flush();
        }
        bo.close();
        bi.close();
    }

    public void read(File file) throws IOException {
        InputStream inputStream = new FileInputStream(file);
        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
        byte[] bytes = new byte[1024];

        int l = 0;
        StringBuilder stringBuilder = new StringBuilder();
        while ((l = bufferedInputStream.read(bytes)) != -1){
            stringBuilder.append(new String(bytes,0,l));
        }
        bufferedInputStream.close();
        inputStream.close();
        System.out.println(stringBuilder.toString());
    }

    public void write(File file) throws IOException {
        OutputStream outputStream = new FileOutputStream(file,true);
        String name = "张三";
        outputStream.write(name.getBytes());
        outputStream.close();
    }

    public void writeBuff(File file) throws IOException {
        OutputStream outputStream = new FileOutputStream(file,true);
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
        String name = "张三";
        bufferedOutputStream.write(name.getBytes());
        bufferedOutputStream.close();
        outputStream.close();
    }


}
